using System.Collections.Generic;
using System.IO;
using Sedge.Core.Execution.ReportElements;

namespace Sedge.Core.Configuration.Report
{
	public class FileSourceBase : ISource
	{
		private readonly string _path;
		private readonly bool _extendedMode;
		private readonly bool _recursive;
		private ReportFileList _reportFileList;

		public string Caption { get; set; }

		public FileSourceBase(string path, bool extendedMode, bool recursive)
		{
			_path = path;
			_extendedMode = extendedMode;
			_recursive = recursive;
		}

		public void PrepareSource(Suite suite)
		{
			_reportFileList = new ReportFileList(suite, _extendedMode);

			List<string> filesToReport = new List<string>();
			if (_path.Contains("*"))
			{
				string path = Services.FileSystem.Path.GetDirectoryName(_path);
				string pattern = Services.FileSystem.Path.GetFileName(_path);

				try
				{
					string[] files = Services.FileSystem.Directory.GetFiles(path, pattern,
						_recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
					filesToReport.AddRange(files);
				}
				catch
				{
					// nothing to do
				}
			}
			else
			{
				filesToReport.Add(_path);
			}

			_reportFileList.AddFiles(filesToReport, FileType.File);
		}

		public List<IReportElement> GetReportElements()
		{
			_reportFileList.Header = Caption;
			return new List<IReportElement> { _reportFileList };
		}

	}
}